package es.android.alertes.client;

import es.android.alertes.R;
import es.android.alertes.objects.AlertaEntity;
import es.android.alertes.objects.ListAdapter;
import greendroid.app.GDActivity;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.android.dataframework.DataFramework;
import com.android.dataframework.Entity;

public class ListaAlertasActivity extends GDActivity {
	
	private ListView lista;
	private ListAdapter adapter;
	private Context ctx;
	
	private ArrayList<AlertaEntity> alertas;
	
	private static String TABLE = "alerta";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ctx = this;
		try
        {
        	DataFramework.getInstance().open(this, "es.android.alertes");
        } catch (Exception e){
        	e.printStackTrace();
        }
		setActionBarContentView(R.layout.lista);
		
		lista = (ListView) findViewById(R.id.lista);
		
		List<Entity> entidades = DataFramework.getInstance().getEntityList(TABLE, null, "nivell DESC");
        
        alertas = new ArrayList<AlertaEntity>();
        
        for (int i = 0; i < entidades.size(); i++)
        {
        	alertas.add(new AlertaEntity().convertFromEntity(entidades.get(i)));
        }
		
        adapter = new ListAdapter(this, android.R.id.text1, alertas);
        
        lista.setAdapter(adapter);
        
        lista.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> ada, View v, int position,
					long id) {
				Intent newIntent = new Intent(ctx, Mapctivity.class);
				newIntent.putExtra("CODIGO", adapter.getItem(position).getCodigo());
				startActivity(newIntent);
			}
		});
	}

}
